package com.lnlyj.media.base;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.snackbar.Snackbar;

/**
 * Created by Wanglei on 2019/4/18.
 */

public abstract class BaseActivity extends AppCompatActivity implements IBaseInterface {

    private int layoutId;
    private View root;

    private Handler mainHandler = new Handler();

    public BaseActivity(int layoutId) {
        this.layoutId = layoutId;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        root = View.inflate(this, layoutId, null);
        setContentView(root);

        initView();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mainHandler != null) {
            mainHandler.removeCallbacksAndMessages(null);
        }
        mainHandler = null;
    }

    public abstract void initView();

    public abstract void initData();

    protected void setSystemDarkColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //获取窗口区域
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

            //设置显示为白色背景，黑色字体
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }

    public void snackMsg(String msg) {
        Snackbar.make(root, msg, Snackbar.LENGTH_SHORT).show();
    }

    public void snackMsg(@StringRes int resId) {
        snackMsg(getString(resId));
    }

    public void postDelayed(Runnable runnable, int delayed) {
        mainHandler.postDelayed(runnable, delayed);
    }

    public void removeCallback(Runnable runnable) {
        mainHandler.removeCallbacks(runnable);
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {
    }

    @Override
    public String getEditString(@IdRes int resId) {
        View view = findViewById(resId);
        if (view instanceof TextView) {
            return ((TextView) view).getText().toString().trim();
        }
        return null;
    }
}
